<?php

Route::view('test-helper', 'test.index');

Route::any('api/test/helper', function () {
    $largeGroupId = Request::get('large_group_id');
    $action = Request::get('action');
//    $env = env('APP_ENV', '');
    $env = \Illuminate\Support\Facades\App::environment();
    $canUseTestHelper = in_array($env, ['local', 'ci', 'tmp']);
    if ($action == '检查运行环境') {
        return response()->json(['ok' => true, 'msg' => "运行环境为 [$env], " . ($canUseTestHelper ? '能' : '不能') . "使用测试助手"]);
    } elseif (!$canUseTestHelper) {
        return response()->json(['ok' => false, 'msg' => "运行环境为 [$env], 不能使用测试助手"]);
    }

    $testSvc = new \App\Services\TestService();

    if ($action == '重置到分配评审阶段') {
        $testSvc->resetSystemDataToAssignProjects($largeGroupId);
    } elseif ($action == '重置到网评待组长审核状态') {
        $testSvc->resetSystemDataToExpertReviewToLeaderReview($largeGroupId);
    } elseif ($action == '填充并提交专家评分') {
        $testSvc->autoFillAndSubmitScores($largeGroupId);
    } elseif ($action == '大组长审核专家评分') {
        $testSvc->autoReviewExpertReviewResult($largeGroupId);
    } elseif ($action == '委员待定区投票') {
        $testSvc->autoVote1($largeGroupId);
    } elseif ($action == '委员投票') {
        $testSvc->autoVote($largeGroupId);
    } elseif ($action == '选择答辩项目') {
        $testSvc->autoSelectDefenseProjects($largeGroupId);
    } elseif ($action == '录入答辩结果') {
        $testSvc->autoUpdateDefenseAwardLevel($largeGroupId);
    } elseif ($action == '重置到形式审查阶段') {
        $testSvc->resetSystemDataToFormalReview();
    } elseif ($action == '自动完成形式审查') {
        $testSvc->allProjectAdoptFormalReview();
    } elseif ($action == '自动进行专家评审') {
        $testSvc->autoExpertReview($largeGroupId);
    } elseif ($action == '提交所有专家的评审结果') {
        $testSvc->submitExpertReview($largeGroupId);
    } elseif ($action == '重置到网评阶段') {
        $testSvc->resetSystemDataToReview($largeGroupId);
    } elseif ($action == '自动填充专家组回避关键字') {
        $testSvc->autoFillFilterKeywords($largeGroupId);
    } elseif ($action == '自动设置所有专家能够参与评审') {
        $testSvc->setExpertReviewCanAttend($largeGroupId);
    }

    return response()->json(['ok' => true]);
});


/*

http://localhost:8003/api/testdata/application

导出表
通过形式审查项目名称	完成人	完成单位

SELECT project_name as '通过形式审查项目名称',`name` as '完成人',`company` as '完成单位' FROM aa_test_from_data;


CREATE TABLE `aa_test_from_data` (
  `id` bigint(22) NOT NULL AUTO_INCREMENT,
  `project_name` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `company` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


 */
Route::any('api/testdata/application', function () {
    $largeGroupId = Request::get('large_group_id');
    $action = Request::get('action');
//    $env = env('APP_ENV', '');

    exit();

    /*
     applications
    award_and_expert_group_relations
    application_groups
    application_form_data
     * */

    $sql = <<<SQL
select `A`.id,form_data from `applications` as `A` 
left join `award_and_expert_group_relations` as `B` on `A`.`id` = `B`.`award_id` 
left join `application_groups` as `C` on `B`.`award_group_id` = `C`.`id` 
left join application_form_data f on f.id=A.id
where `status_id` in ("C1") order by `A`.`submitted_at` asc;
SQL;

    $getData = DB::select($sql);

    DB::table('aa_test_from_data')->delete();

    $list = [];
    foreach ($getData as $index => $datum) {
        if (empty($datum->form_data)) {
            continue;
        }
        $form_data = json_decode($datum->form_data, true);
        if (empty($form_data)) {
            continue;
        }

//      $form_data.basic_situation.project_name
//      $form_data.finish_people[].finish_people
//      $form_data.finish_people[].company

        $project_name = '';
        if (!empty($form_data['basic_situation']['project_name'])) {
            $project_name = $form_data['basic_situation']['project_name'];
        }

        if (empty($form_data['finish_people'])) {
            continue;
        }
        $finish_people = $form_data['finish_people'];

        $add = [];
        foreach ($finish_people as $indexNote => $itemNote) {
            $add[] = [
                'project_name' => $project_name,
                'name' => $itemNote['name'] ?? '',
                'company' => $itemNote['company'] ?? '',
            ];
//            DB::table('aa_test_from_data')->addAll($add);
        }

        DB::table('aa_test_from_data')->insert($add);
    }

    return response()->json(['ok' => true]);
});